<!--  -->
<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCateDialog"
            >添加分类</el-button
          >
        </el-col>
      </el-row>

      <tree-table
        :data="cateList"
        show-index
        stripe
        border
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        class="tree-table"
      >
        <template slot-scope="scope" slot="isOk">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false"></i>
          <i class="el-icon-error" v-else></i>
        </template>
        <template slot-scope="scope" slot="order">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <template slot-scope="scope" slot="operation">
          <el-button icon="el-icon-edit" type="primary" size="mini" @click="showEditorDialog(scope.row.cat_id)"
            >编辑</el-button
          >
          <el-button icon="el-icon-delete" type="danger" size="mini" @click="deleteCate(scope.row.cat_id)"
            >删除</el-button
          >
        </template>
      </tree-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="addCateDialogClose"
    >
      <el-form
        :model="addCateForm"
        :rules="addCaterules"
        ref="addCateFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="selectValue"
            :options="parentsCateList"
            :props="cascaderProps"
            @change="handleChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="编辑分类"
      :visible.sync="editorCateDialogVisible"
      width="50%"
    >
      <el-form
        :model="editorCateForm"
        ref="editorCateFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editorCateForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editorCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editorCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Cate',
  data () {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      cateList: [],
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operation'
        }
      ],
      total: 0,
      addCateDialogVisible: false,
      addCateForm: {
        cat_name: ''
      },
      addCaterules: {
        cat_name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }
        ]
      },
      selectValue: [],
      parentsCateList: [],
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      editorCateDialogVisible: false,
      editorCateForm: {},
      cat_id: ''
    }
  },

  components: {},
  created () {
    this.getCateList()
  },

  methods: {
    async getCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      console.log(res)
      if (res.meta.status !== 200) { return this.$message.error('获取列表数据失败！') }

      this.cateList = res.data.result
      this.total = res.data.total
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getCateList()
    },
    async showAddCateDialog () {
      const { data: res } = await this.$http.get('categories', {
        type: 2
      })
      if (res.meta.status !== 200) { return this.$message.error('获取父级分类失败！') }
      this.parentsCateList = res.data
      console.log(res)
      this.addCateDialogVisible = true
    },
    handleChange () {
      console.log(this.selectValue)
    },
    addCate () {
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) return
        if (this.selectValue.length !== 0) {
          const { data: res } = await this.$http.post('categories', {
            cat_pid: this.selectValue[this.selectValue.length - 1],
            cat_name: this.addCateForm.cat_name,
            cat_level: this.selectValue.length
          })
          if (res.meta.status !== 201) { return this.$message.error('添加分类失败！！') }
          this.$message.success('添加分类成功！')
        } else {
          const { data: res } = await this.$http.post('categories', {
            cat_pid: 0,
            cat_name: this.addCateForm.cat_name,
            cat_level: 0
          })
          if (res.meta.status !== 201) { return this.$message.error('添加分类失败！！') }
          this.$message.success('添加分类成功！')
        }
        this.getCateList()
        this.addCateDialogVisible = false
      })
    },
    addCateDialogClose () {
      this.$refs.addCateFormRef.resetFields()
      this.selectValue = ''
    },
    async showEditorDialog (id) {
      const { data: res } = await this.$http.get('categories/' + id)
      if (res.meta.status !== 200) return this.$message.error('获取分类失败！')
      this.editorCateForm = res.data
      this.cat_id = res.data.cat_id
      this.editorCateDialogVisible = true
    },
    async editorCate () {
      if (!this.editorCateForm.cat_name) return
      const { data: res } = await this.$http.put('categories/' + this.cat_id, {
        cat_name: this.editorCateForm.cat_name
      })
      if (res.meta.status !== 200) return this.$message.error('编辑分类失败！')
      this.$message.success('编辑分类成功!!')
      this.getCateList()
      this.editorCateDialogVisible = false
    },
    async deleteCate (id) {
      const deleteConfirm = await this.$confirm(
        '此操作将永久删除该商品分类, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      // console.log(deleteConfirm);
      if (deleteConfirm !== 'confirm') { return this.$message.info('已取消删除该商品分类！') }
      const { data } = await this.$http.delete('categories/' + id)
      if (data.meta.status !== 200) { return this.$message.error('删除该商品分类失败！') }
      this.editorDialogVisible = false
      this.$message.success('删除该商品分类成功！')
      this.getCateList()
    }
  }
}
</script>
<style scoped>
.tree-table {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
